var _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(t) {
    return typeof t;
} : function(t) {
    return t && "function" == typeof Symbol && t.constructor === Symbol && t !== Symbol.prototype ? "symbol" : typeof t;
}, _tools = require("../../../utils/tools");

function _toConsumableArray(t) {
    if (Array.isArray(t)) {
        for (var e = 0, r = Array(t.length); e < t.length; e++) r[e] = t[e];
        return r;
    }
    return Array.from(t);
}

var g, tab, pages, app = getApp(), page = app.page, request = app.util.request, route = app.route;

function getOrderList(t) {
    var o = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : 1;
    1 == o && g.setData({
        data: []
    }), request({
        url: "MyLawOrder",
        data: {
            order_state: t || g.data.TabCur,
            user_id: app.userInfo.id || wx.getStorageSync("userInfo").id,
            page: o,
            number: app.config.orders_number
        },
        showLoading: !1,
        success: function(t) {
            var e = g.data.data;
            if (g.data.system && "1" == g.data.system.is_tuikuan && g.setData({
                TabItem: [ {
                    title: "全部",
                    id: 0
                }, {
                    title: "待付款",
                    id: 1
                }, {
                    title: "待服务",
                    id: 2
                }, {
                    title: "待确认",
                    id: 3
                }, {
                    title: "待评价",
                    id: 4
                }, {
                    title: "已完成",
                    id: 5
                }, {
                    title: "退款",
                    id: 7
                } ]
            }), console.log(t), t && "object" == (void 0 === t ? "undefined" : _typeof(t))) for (var r, a = 0; a < t.length; a++) 6 == t[a].order_state ? t.splice(a, 1) : (r = (r = (0, 
            _tools.formatTime)(t[a].submit_time)).slice(0, r.length - 3), t[a].submit_time = r);
            e && 1 != o && (t = [].concat(_toConsumableArray(e), _toConsumableArray(t))), g.setData({
                data: t
            }), wx.stopPullDownRefresh(), t.length && o * app.config.orders_number >= t[0].num && g.setData({
                isLoad: !0
            });
        }
    });
}

page({
    data: {
        reminderStatus: 2,//0-不可催单 1-可催单 2-更换中 3-频繁点击
        isShowReminderTip: false,
        tipType: 0,
        TabCur: 0,
        scrollLeft: 0,
        TabItem: [ {
            title: "全部",
            id: 0
        }, {
            title: "待付款",
            id: 1
        }, {
            title: "待服务",
            id: 2
        }, {
            title: "待确认",
            id: 3
        }, {
            title: "待评价",
            id: 4
        }, {
            title: "已完成",
            id: 5
        }, {
            title: "退款",
            id: 7
        } ],
        no_orders: app.srcRoot + "no_order.png",
        isLoad: !1,
        reminderOrder: {},
        tipMsg: '',
        kefu: ''
    },
    //催单
    
    onLoad: function(t) {
        g = this, pages = 1, tab = t && t.index ? t.index : 0, g.setData({
            TabCur: tab
        });
        request({
          url: "ServiceTel",
          data: {},
          success:function(t){
            g.setData({
              kefu: t
            })
          }
        })
    },
    onPullDownRefresh: function() {
        g.setData({
            isLoad: !1
        }), pages = 1, getOrderList();
    },
    tabSelect: function(t) {
        g.setData({
            TabCur: parseInt(t.currentTarget.dataset.id),
            scrollLeft: 60 * (t.currentTarget.dataset.id - 1),
            isLoad: !1
        }, function() {
            pages = 1, getOrderList();
        });
    },
    cancelOrder: function(t) {
        (0, _tools.subscribe)("USER"), t = t.currentTarget.dataset.item.id, request({
            url: "LawCancelOrder",
            data: {
                order_id: t
            },
            success: function(t) {
                t = t ? "取消成功" : "错误:" + t;
                wx.showToast({
                    icon: "none",
                    title: t
                }), g.setData({
                    isLoad: !1
                }), pages = 1, getOrderList();
            }
        });
    },
    payOrder: function(t) {
        var e = (t = t.currentTarget.dataset.item).id, r = t.price;
        request({
            url: "LawOrderPay",
            data: {
                user_id: app.userInfo.id || wx.getStorageSync("userInfo").id,
                order_id: e
            },
            success: function(t) {
                console.log("回调支付信息：", t), (0, _tools.wxPay)({
                    data: t,
                    success: function(t) {
                        app.item = {
                            title: "支付成功",
                            content: "订单支付成功 ￥" + r,
                            img: "pay_ok",
                            button: [ {
                                title: "查看订单",
                                url: "orders_info",
                                a: {
                                    order_id: e,
                                    type: 0
                                }
                            }, {
                                title: "返回首页",
                                url: "loading",
                                type: "reLaunch"
                            } ]
                        }, route({
                            type: "redirect",
                            url: "result"
                        });
                    }
                });
            }
        });
    },
    callPhone: function(t) {
        (0, _tools.subscribe)("USER"), t = t.currentTarget.dataset.item.law_phone, wx.makePhoneCall({
            phoneNumber: t
        });
    },
    service_log: function(t) {
        (0, _tools.subscribe)("USER"), t = t.currentTarget.dataset.item, route({
            type: "navigate",
            url: "service_log",
            params: {
                order_id: t.id,
                state: t.order_state
            }
        });
    },
    evaluate: function(t) {
        (0, _tools.subscribe)("USER"), t = t.currentTarget.dataset.item, route({
            type: "navigate",
            url: "evaluate",
            params: {
                order_id: t.id,
                type: 0,
                id: t.law_id
            }
        });
    },
    orderOk: function(r) {
        (0, _tools.subscribe)("USER");
        (r = r.currentTarget.dataset).index;
        r = r.item, request({
            url: "LawOrderAachieve",
            data: {
                order_id: r.id
            },
            success: function(t) {
                var e = t ? "已完成" : "错误:" + t;
                wx.showToast({
                    icon: "none",
                    title: e
                }), t && setTimeout(function() {
                    getOrderList(g.data.TabCur), route({
                        type: "navigate",
                        url: "evaluate",
                        params: {
                            order_id: r.id,
                            type: 0,
                            id: r.law_id
                        }
                    });
                }, 1500);
            }
        });
    },
    onShow: function() {
        getOrderList(g.data.TabCur);
    },
    toInfo: function(t) {
        if(t.currentTarget.dataset.item.reminder_status == 3){
          return false
        }
        (0, _tools.subscribe)("USER"), route({
            type: "navigate",
            url: "orders_info",
            params: {
                order_id: t.currentTarget.dataset.item.id,
                type: 0
            }
        });
    },
    chat: function(t) {
        (0, _tools.subscribe)("USER"), route({
            type: "navigate",
            url: "chat_info",
            params: {
                toid: t.currentTarget.dataset.item
            }
        });
    },
    onReachBottom: function() {
        !g.data.isLoad && pages++ && getOrderList(g.data.TabCur, pages);
    },
    openReminder: function(t){
      let that = this
      let index = t.currentTarget.dataset.index
      let info = g.data.data[index]
      if(info.reminder_status == 3){
        return false
      }
      that.setData({
        isShowReminderTip: true,
        tipType: 1,
        tipMsg: "请稍等，请选择您需要的服务",
        reminderOrder: info
      })
    },
    doReminder: function(t){
      let that = this
      request({
        url: "Reminder",
        data: {
            order_id: that.data.reminderOrder.id,
            type: 1
        },
        success: function(t) {
          console.log(t)
          that.setData({
            isShowReminderTip: false
          })
        },
        fail: function(t) {
          console.log(t)
          that.setData({
            isShowReminderTip: true,
            tipType: 1,
            tipMsg: t.data.data.message,
            reminderOrder: info
          })
        }
      })
    },
    closeReminder:function(){
      this.setData({
        isShowReminderTip: false,
        tipType: 0
      })
    },
    changeLawyer: function(t){
      let that = this
      request({
        url: "Reminder",
        data: {
            order_id: that.data.reminderOrder.id,
            type: 2
        },
        success: function(t) {
          let index = g.data.data.findIndex((item)=>{
            return item.id == that.data.reminderOrder.id
          })
          console.log(11,index,that.data.reminderOrder.id)
          if(index !== -1){
            g.data.data[index].reminder_status = 3
            g.setData({
              data: g.data.data
            })
          }
          that.setData({
            isShowReminderTip: true,
            tipType: 3,
            tipMsg: "小咚已经在为你快马加鞭更换律师中，请您耐心等待"
          })
        },
        fail: function(t) {
          console.log(t)
          that.setData({
            isShowReminderTip: true,
            tipType: 1,
            tipMsg: t.data.message
          })
        }
      })
      this.setData({
        isShowReminderTip: false,
        tipType: 0
      })
    }
});